package com.cxg.fairy.ec.main.personal;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.ec.main.personal.applyInvoice.ApplyInvoiceDelegate;
import com.cxg.fairy.ec.main.personal.balance.BalanceDelegate;
import com.cxg.fairy.ec.main.personal.coupon.AllCouponTabDelegate;
import com.cxg.fairy.ec.main.personal.favor.FavorDelegate;
import com.cxg.fairy.ec.main.personal.help.HelpCenterDelegate;
import com.cxg.fairy.ec.main.personal.history.HistoryDelegate;
import com.cxg.fairy.ec.main.personal.member.MemberDelegate;
import com.cxg.fairy.ec.main.personal.message.MessageCenterDelegate;
import com.cxg.fairy.ec.main.personal.needs.NewGoodsNeedsDelegate;
import com.cxg.fairy.ec.main.personal.order.OrderListEntryDelegate;
import com.cxg.fairy.ec.main.personal.score.ScoreDelegate;
import com.cxg.fairy.ec.pay.DepositDelegate;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.util.log.LatteLogger;
import com.cxg.fairy.util.toasty.Toasty;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;
import com.cxg.fairy.delegates.bottom.BottomItemDelegate;
import com.cxg.fairy.ec.main.personal.address.AddressDelegate;
import com.cxg.fairy.ec.main.personal.profile.UserProfileDelegate;
import com.cxg.fairy.ec.main.personal.settings.SettingsDelegate;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by CFairy
 */

public class PersonalDelegate extends BottomItemDelegate implements ISuccess {

    public static final String ORDER_TYPE = "ORDER_TYPE";

    private Bundle mArgs = null;

    DecimalFormat format = new DecimalFormat("0.00");

    private static final RequestOptions OPTIONS = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .centerCrop()
            .dontAnimate();

    @BindView(R2.id.img_user_avatar)
    CircleImageView imgViewAvatar = null;

    @BindView(R2.id.tv_user_name)
    AppCompatTextView tvUserName = null;

    @BindView(R2.id.itv_user_level_tag)
    AppCompatImageView imgUserLevelTag = null;

    @BindView(R2.id.tv_user_level_name)
    AppCompatTextView tvUserLevelName = null;

    @BindView(R2.id.tv_user_level_up_content)
    AppCompatTextView tvUserLevelUpContent = null;

    @BindView(R2.id.tv_yu_e)
    AppCompatTextView tvYuE = null;

    @BindView(R2.id.tvv_qianbao)
    AppCompatTextView tvQianBao = null;

    @BindView(R2.id.tv_jifen)
    AppCompatTextView tvJifen = null;

    @BindView(R2.id.tv_msCouponGetNum)
    AppCompatTextView tvCouponNum = null;

    @OnClick(R2.id.ll_address)
    void toPersonalAddressMana(){
        getParentDelegate().getSupportDelegate().start(new AddressDelegate());
    }

    @OnClick(R2.id.ll_sys_set)
    void toPersonalSysSet() {
        getParentDelegate().getSupportDelegate().start(new SettingsDelegate());
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_personal;
    }

    @OnClick(R2.id.tv_all_order)
    void onClickAllOrder() {
        mArgs.putInt(ORDER_TYPE, 0);
        startOrderListByType();
    }

    @OnClick(R2.id.ll_pay)
    void onClickPayingOrder() {
        mArgs.putInt(ORDER_TYPE, 1);
        startOrderListByType();
    }

    @OnClick(R2.id.ll_dispatching)
    void onClickDispatching() {
        mArgs.putInt(ORDER_TYPE,2);
        startOrderListByType();
    }

    @OnClick(R2.id.ll_ship)
    void onClickShip() {
        mArgs.putInt(ORDER_TYPE,3);
        startOrderListByType();
    }

    @OnClick(R2.id.ll_youhuiquan)
    void onClicCoupon() {
        getParentDelegate().getSupportDelegate().start(new AllCouponTabDelegate());
    }

    @OnClick(R2.id.ll_evaluate)
    void onClickEvaluate() {
        mArgs.putInt(ORDER_TYPE,4);
        startOrderListByType();
    }

    @OnClick(R2.id.ll_to_refund)
    void onClickToRefund() {
        mArgs.putInt(ORDER_TYPE,5);
        startOrderListByType();
    }

    @OnClick(R2.id.ll_yu_e)
    void onClickBalance() {
        getParentDelegate().getSupportDelegate().start(new BalanceDelegate());
    }

    @OnClick(R2.id.ll_qianbao)
    void onClickBalance1() {
        getParentDelegate().getSupportDelegate().start(new BalanceDelegate());
    }

    @OnClick(R2.id.ll_jifen)
    void onClickScore() {
        getParentDelegate().getSupportDelegate().start(new ScoreDelegate());
    }

    @OnClick(R2.id.ll_shoucang)
    void onClickToFavor() {
        getParentDelegate().getSupportDelegate().start(new FavorDelegate());
    }

    @OnClick(R2.id.ll_lljilu)
    void onClickToHistory() {
        getParentDelegate().getSupportDelegate().start(new HistoryDelegate());
    }

    @OnClick(R2.id.ll_fapiao)
    void onClickToApplyInvoice() {
        getParentDelegate().getSupportDelegate().start(new ApplyInvoiceDelegate());
    }

    @OnClick(R2.id.tv_huiyuanzhongxi)
    void onClickToMemberCenter() {
        getParentDelegate().getSupportDelegate().start(new MemberDelegate());
    }

    @OnClick(R2.id.img_user_avatar)
    void onClickAvatar() {
        getParentDelegate().getSupportDelegate().start(new UserProfileDelegate());
    }

    @OnClick(R2.id.ll_kefuzhongxin)
    void onClickNewNeeds() {
        getParentDelegate().getSupportDelegate().start(new NewGoodsNeedsDelegate());
    }

    @OnClick(R2.id.tv_msg_center)
    void onClickMsgCenter() {
        getParentDelegate().getSupportDelegate().start(new MessageCenterDelegate());
    }


    @OnClick(R2.id.ll_helpcenter)
    void onCickHelpCenter() {
        getParentDelegate().getSupportDelegate().start(new HelpCenterDelegate());
    }

    //充值
    @OnClick(R2.id.tv_chongzhi)
    void onClickDeposit() {
        getParentDelegate().getSupportDelegate().start(new DepositDelegate());
    }

    private void startOrderListByType() {
        final OrderListEntryDelegate delegate = new OrderListEntryDelegate();
        delegate.setArguments(mArgs);
        getParentDelegate().getSupportDelegate().start(delegate);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mArgs = new Bundle();
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        initData();
    }

    public void initData() {
        RestClient.builder()
                .url("ms/getMsCenter")
                .params("uuid", AccountManager.getSignUUID())
                .loader(getContext())
                .success(this)
                .build()
                .post();
    }

    @Override
    public void onSuccess(String response) {
        int resCode = JSON.parseObject(response).getInteger("code");
        LatteLogger.e("response=",response);
        if (resCode == 1) {
            JSONObject data = JSON.parseObject(response).getJSONObject("data");
            JSONObject person = data.getJSONObject("person");
            String headIco = person.getString("head_ico");
            BigDecimal balance = data.getBigDecimal("balance");
            int point = data.getInteger("point");
            int msCouponGetNum = data.getInteger("msCouponGetNum");
            int msGrade = person.getInteger("msGrade");
            String levelName = person.getString("msLevel");
            String userName = person.getString("accountName");
            String diffWithNextMsLevel = person.getString("diffWithNextMsLevel");
            tvUserName.setText(userName);
            tvYuE.setText(format.format(balance));
            tvQianBao.setText(format.format(balance));
            tvJifen.setText(point+"");
            tvCouponNum.setText(msCouponGetNum+"");
            tvUserLevelName.setText(levelName);
            tvUserLevelUpContent.setText(diffWithNextMsLevel);
            Glide.with(getContext())
                    .load(headIco)
                    .apply(OPTIONS)
                    .into(imgViewAvatar);
            switch (msGrade) {
                case 1:
                    imgUserLevelTag.setImageResource(R.mipmap.level1);
                    break;
                case 2:
                    imgUserLevelTag.setImageResource(R.mipmap.level2);
                    break;
                case 3:
                    imgUserLevelTag.setImageResource(R.mipmap.level3);
                    break;
                case 4:
                    imgUserLevelTag.setImageResource(R.mipmap.level4);
                    break;
                case 5:
                    imgUserLevelTag.setImageResource(R.mipmap.level5);
                    break;
                case 6:
                    imgUserLevelTag.setImageResource(R.mipmap.level6);
                    break;
                case 7:
                    imgUserLevelTag.setImageResource(R.mipmap.level7);
                    break;
                default:
                    break;
            }

        } else {
            Toasty.error(getContext(), "网络错误").show();
        }


    }
}
